package com.jxsd.edu.cn.struts2.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.jxsd.edu.cn.struts2.models.UserInfo;
import com.jxsd.edu.cn.struts2.serviceImpl.UserServiceImpl;
import com.jxsd.edu.cn.struts2.services.UserService;
import com.opensymphony.xwork2.ActionContext;

public class UserAction 
{
   private   UserInfo  user;
   private   String  newPassword;
   
   
   private   File    upload;
   private   String  uploadContentType;
   private   String  uploadFileName;
   private   String  savePath;
   
   private   UserService  userService=new  UserServiceImpl();
   
   
   
   /**
    * hander  the  request  for  changing  passsword
    * */
   public  String  doChangePwd()
   {
	   UserInfo  newUser=new  UserInfo();
	   newUser.setUserPassword(newPassword);
	   newUser.setUserId(user.getUserId());
	   return   userService.doChangePwd(Integer.toString(user.getUserId()), user.getUserPassword(), newPassword);
   }
   /**
    * hander  the  request  for  updating  image
 * @throws IOException 
    * */
   public  void  doUpdateImage() throws IOException
   {
	    HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		
	    userService.doUpdateImage(user);
	    
	    PrintWriter  writer=response.getWriter();
 		writer.write("user  image  update  success!");
 		writer.flush();
 		writer.close();
   }
   
   /**
    * hander  the  request  for updating  user information
 * @throws IOException 
    * */
   public  String  doUpdateInfo() throws Exception
   {
		String   fosPath=doUpload();
		
		if(!"".equals(fosPath)&&null!=fosPath)
		{
			user.setImage(fosPath);	
		}
		
		int  userId=(Integer)ActionContext.getContext().getSession().get("userId");
		user.setUserId(userId);
	    userService.doUpdateInfo(user);
	    return "success";
   }
   /**
    * get the  userInfo  of  the  user
    * */
   public  String  QueryUserInfo()
   {
	   
	   String  userNickName=(String)ActionContext.getContext().getSession().get("nickName");
	   user=userService.getUserInfo("nickName", userNickName);
	   return  "success";
   }
   
   /**
    * hander  the  upload  file
 * @throws FileNotFoundException 
    * */
   private  String  doUpload() throws Exception
   {
	   String  fosPath=null;
	   
	   if(null==upload)
	   {
		   return  "";
	   }
	   
	// 对于上传文件的存放地址来建立一个输出流
		fosPath=getSavePath()+"/"+ System.currentTimeMillis()
			+ uploadFileName.substring(uploadFileName.lastIndexOf("."),
					uploadFileName.length());
		
		String path = ServletActionContext.getServletContext()     
                .getRealPath("/")+fosPath;
	   FileOutputStream fos = new FileOutputStream(path);
	   FileInputStream fis = new FileInputStream(getUpload());
	   byte[] buffer = new byte[1024];
	   int len = 0;
	   while ((len = fis.read(buffer)) > 0)
	   {
	    fos.write(buffer , 0 , len);
	   }
	   
	   fis.close();
	   fos.close();
	   
	   return  fosPath;
   }
   
   
   
   /**
    * hander  the  request  for  retaking  password
    * */
   public  String  doRetakePwd()
   {
	   return  null;
   }
   
   public UserInfo getUser() 
   {
	  return user;
   }

  public void setUser(UserInfo user) 
   {
	 this.user = user;
   }
public String getUploadContentType() {
	return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
	this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
	return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
	this.uploadFileName = uploadFileName;
}
public String getSavePath() {
	return savePath;
}
public void setSavePath(String savePath) {
	this.savePath = savePath;
}
public File getUpload() {
	return upload;
}
public void setUpload(File upload) {
	this.upload = upload;
}

}
